package org.bapoint.reactadmin.exception;

import jakarta.mail.internet.AddressException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bapoint.reactadmin.entity.vo.AjaxResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


import java.io.IOException;
import java.util.ResourceBundle;

/**
 * @class RestExceptionHandler
 * @Author wyp
 * @Description 控制层异常处理
 * @Date 2022/12/19
 * @Version 1.0
 */
@ControllerAdvice
public class RestExceptionHandler {
//    protected static final ResourceBundle RES = ResourceBundle.getBundle("strings/exception");
    private static final Log logger = LogFactory.getLog(RestExceptionHandler.class);

    @ExceptionHandler({AppException.class})
    public ResponseEntity<AjaxResult<?>> petsLoverExceptionHandler(AppException ex) {
        logger.error("petsLover exception", ex);
        AjaxResult<?> result = new AjaxResult<>();
        result.setMessage(ex.getMessage());
        result.setCode(ex.getErrorCode());
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<AjaxResult<?>> noMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        logger.error("MethodArgumentNotValidException exception", ex);
        AjaxResult<?> result = new AjaxResult<>();
        result.setMessage("请按照规则填写后重试!");
        result.setCode("500");
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }

    @ExceptionHandler({AddressException.class})
    public ResponseEntity<AjaxResult<?>> noAddressException(AddressException ex){
        logger.error("AddressException exception", ex);
        AjaxResult<?> result = new AjaxResult<>();
        result.setMessage("请按规则填写邮箱!");
        result.setCode("500");
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }

    @ExceptionHandler({IOException.class})
    public ResponseEntity<AjaxResult<?>> noIOException(IOException ex){
        logger.error("IOException exception", ex);
        AjaxResult<?> result = new AjaxResult<>();
        result.setMessage("系统繁忙!请稍后再试~");
        result.setCode("500");
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }

}